package basic.chapter4;

/**
 * @author Kevin
 * @date 2020/2/20 20:47
 */
public class Account1 {
    private Object lock;
    private int balance;
    private Account1(){};
    // 创建Account1时传入同一个lock对象
    public Account1(Object lock) {
        this.lock = lock;
    }
    //转账
    void transfer(Account1 target, int amt) {
        //此处检查所有对象的共享锁
        synchronized (lock) {
            if (this.balance >= amt) {
                this.balance -= amt;
                target.balance += amt;
            }
        }
    }
}
